package com.surfilter.massdata.spark.util.ipcheck;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.surfilter.massdata.spark.bean.ipcheck.ErrIp;
import com.surfilter.massdata.spark.bean.ipcheck.IpBean;
import com.surfilter.massdata.spark.bean.ipcheck.ObjectInfo;



/** 
* @author  hutao
* @date 创建时间：2017年5月27日 下午3:14:52 
* @Description：扫描来源库，参照分配库
* @version 1.0 
* 
**/
public class IpSourceStorageScanner {

	
	public static ObjectInfo scanSourceStorage(List<IpBean> reduceSourceList, List<IpBean> reduceAllocateList, Map<Long, String> bsdwMap) throws IOException, InterruptedException{
		ObjectInfo objectInfo=new ObjectInfo();
		objectInfo.setResult(true);
		List<IpBean> bbdw_lydw_list = reduceSourceList;
		for(IpBean bbdw_lydw : bbdw_lydw_list){
			if(!new Boolean(false).equals(bbdw_lydw.getIsPass())){
//				log.info(ip + "|" + bbdw_lydw.toString());
				boolean flag1 = false; //表示分配库里有来源库对应的IP
				boolean flag2 = true; //表示A分配给C不是自带
				boolean flag3 = false; //表示A分配给C
				//A来源于B
				Long lyId = bbdw_lydw.getLyid();
				Long bbId = bbdw_lydw.getBbdw_id(); 
				//如果这个IP的来源单位不是外省的
				//那么就关注有没有把这个IP分配出去(B分配给A);并且分配库里有A分配的记录，不可以是自带
				if(bsdwMap.get(lyId) != null){
//					log.info("本省");
					List<IpBean> alloList = reduceAllocateList;
					if(alloList == null || alloList.size() == 0){
						//来源库有，分配库没有此ip，属于分配漏报或来源越界
//						log.info("来源库有，分配库没有此ip" + key.toString() + "|" + bbdw_lydw.toString());
//						redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//						log.info("scanner75冲突ip:"+newIp);
//						bbdw_lydw.setIsPass(false);
						ErrIp errIp = new ErrIp();
						errIp.setRegionIds(bbdw_lydw.getStorageType() + "_" + bbdw_lydw.getTflag() + "_" + bbdw_lydw.getId());
						errIp.setJsonStr(bbdw_lydw.getConflictMsg());
						if(bbdw_lydw.getStorageType().equals(IpCheckConstants.BEIAN)){
							errIp.setBbdws(bbId + "," + lyId);
							errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR.getTypeValue());
							errIp.setStorageType(IpCheckConstants.BEIAN);
							objectInfo.setResult(false);
							objectInfo.setErrInfo(errIp);
							return objectInfo;
						}else if(bbdw_lydw.getStorageType().equals(IpCheckConstants.UPLOAD)){
							errIp.setBbdws(bbId + "," + lyId);
							errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR2.getTypeValue());
							errIp.setStorageType(IpCheckConstants.UPLOAD);
							objectInfo.setResult(false);
							objectInfo.setErrInfo(errIp);
							return objectInfo;
							
						}
						continue;
					}else{
						//来源库有，扫描看看分配库有没有（B分配给A）;并且A分配给C,且状态不可以是自带 ----modify:2015-12-08 
						//先扫描A分配给C,且状态不可以是自带 --modify:2015-12-08
						for(IpBean allocate : alloList){
							if(!new Boolean(false).equals(allocate.getIsPass())){
//							log.info("分配库有此ip" + key.toString() + "|" + allocate.toString());
								if(bbId.equals(allocate.getBbdw_id())){
									if(IpAllocateType.HOLDING_BY_SELF.getTypeValue().equals(allocate.getFpfs())){
//										redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//										log.info("scanner111冲突ip:"+newIp);
//										allocate.setIsPass(false);
//										bbdw_lydw.setIsPass(false);
										String bbdws = bbId.toString();
										String regionIds = bbdw_lydw.getStorageType() + "_" + bbdw_lydw.getTflag() + "_" + bbdw_lydw.getId() + "," + allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId();
										ErrIp errIp = new ErrIp();
										errIp.setBbdws(bbdws);
										errIp.setRegionIds(regionIds);
										errIp.setJsonStr(bbdw_lydw.getConflictMsg()+"\001"+allocate.getConflictMsg());
										if(bbdw_lydw.getStorageType().equals(IpCheckConstants.BEIAN)){
											errIp.setConflictType(ConflictType.IP_SEG_ERROR.getTypeValue());
											errIp.setStorageType(IpCheckConstants.BEIAN);
											objectInfo.setResult(false);
											objectInfo.setErrInfo(errIp);
											return objectInfo;
										}else if(bbdw_lydw.getStorageType().equals(IpCheckConstants.UPLOAD)){
											errIp.setConflictType(ConflictType.IP_SEG_ERROR2.getTypeValue());
											errIp.setStorageType(IpCheckConstants.UPLOAD);
											objectInfo.setResult(false);
											objectInfo.setErrInfo(errIp);
										}
										flag2 = false;
										break;
									}else{
										flag3 = true;
										break;
									}
								}else{
									flag3 = false;
								}
							}else{
								continue;
							}
						}// end for
						
						if(flag2 == false){
							//此时已检测出该ip在当前报备单位下属于分配属性错误，结束循环
							continue;
						}else if(flag3 == true){
							//需要继续检测B有没有分配给A
//							log.info("需要继续检测B有没有分配给A");
//							log.info("source ip:" + bbdw_lydw.toString() );
							for(IpBean allocate : alloList){
//								log.info(allocate.toString());
								if(!new Boolean(false).equals(allocate.getIsPass())){
									if(allocate.getFpfs().equals(IpAllocateType.RE_ALLOCATE.getTypeValue())){
										if(lyId.equals(allocate.getBbdw_id()) && bbId.equals(allocate.getFpdx())){
											flag1 = true;
											break;
										}else{
											flag1 = false;
										}
									}else{
										flag1 = false;
									}
								}
							}// end for
//							log.info("flag1 again:" + flag1);
							if(flag1 == false){
								//说明没有找到B分配给A 属于分配漏报或来源越界
//								bbdw_lydw.setIsPass(false);
//								redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//								log.info("scanner171冲突ip:"+newIp);
								ErrIp errIp = new ErrIp();
								errIp.setRegionIds(bbdw_lydw.getStorageType() + "_" + bbdw_lydw.getTflag() + "_" + bbdw_lydw.getId());
								errIp.setJsonStr(bbdw_lydw.getConflictMsg());
								if(bbdw_lydw.getStorageType().equals(IpCheckConstants.BEIAN)){
//									if(bbId > lyId){
										errIp.setBbdws(bbId + "," + lyId);
//									}else{
//										errIp.setBbdws(lyId + "," + bbId);
//									}
									errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR.getTypeValue());
									errIp.setStorageType(IpCheckConstants.BEIAN);
									objectInfo.setResult(false);
									objectInfo.setErrInfo(errIp);
									return  objectInfo;
								}else if(bbdw_lydw.getStorageType().equals(IpCheckConstants.UPLOAD)){
//									if(bbId > lyId){
										errIp.setBbdws(bbId + "," + lyId);
//									}else{
//										errorIpSeg.setBbdws(lyId + "," + bbId);
//									}
									errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR2.getTypeValue());
									errIp.setStorageType(IpCheckConstants.UPLOAD);
									objectInfo.setResult(false);
									objectInfo.setErrInfo(errIp);
									return  objectInfo;
								}
							}
							continue;
						}else if(flag3 == false){
							//没有找到A分配给C，属于分配漏报或来源越界 ；
//							redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
							ErrIp errIp = new ErrIp();
							errIp.setBbdws(bbId.toString());
							errIp.setRegionIds(bbdw_lydw.getStorageType() + "_" + bbdw_lydw.getTflag() + "_" + bbdw_lydw.getId());
							errIp.setJsonStr(bbdw_lydw.getConflictMsg());
							if(bbdw_lydw.getStorageType().equals(IpCheckConstants.BEIAN)){
								errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR.getTypeValue());
								errIp.setStorageType(IpCheckConstants.BEIAN);
								objectInfo.setResult(false);
								objectInfo.setErrInfo(errIp);
								return  objectInfo;
							}else if(bbdw_lydw.getStorageType().equals(IpCheckConstants.UPLOAD)){
								errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR2.getTypeValue());
								errIp.setStorageType(IpCheckConstants.UPLOAD);
								objectInfo.setResult(false);
								objectInfo.setErrInfo(errIp);
								return  objectInfo;
								
							}
							continue;
						}
					}// end if-else
				}else{
					//如果是外省
					//那么就只关注A有没有分配给C，或者A也可以是自带 modify:2015-12-07
//					log.info("外省");
					List<IpBean> alloList = reduceAllocateList;
					for(IpBean allocate : alloList){
						if(!new Boolean(false).equals(allocate.getIsPass())){
//							log.info("allocate :" + allocate.toString());
							if(bbId.equals(allocate.getBbdw_id())){
								flag3 = true;
								break;
							}else{
								flag3 = false;
							}
						}
					}// end for
					
					if(flag2 == false){
						//此时已检测出该ip在当前报备单位下属于分配属性错误，结束循环
						continue;
					}else if(flag3 == true){
						//找到了A分配给C，也可以是自带  --modify:2015-12-07
						continue;
					}else if(flag3 == false){
						//没有找到A分配给C，属于分配漏报或来源越界 
//						log.info("没有找到A分配给C");
//						redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//						log.info("scanner271冲突ip:"+newIp);
//						bbdw_lydw.setIsPass(false);
						ErrIp errIp = new ErrIp();
						errIp.setBbdws(bbId.toString());
						errIp.setRegionIds(bbdw_lydw.getStorageType() + "_" + bbdw_lydw.getTflag() + "_" + bbdw_lydw.getId());
						errIp.setJsonStr(bbdw_lydw.getConflictMsg());
						if(bbdw_lydw.getStorageType().equals(IpCheckConstants.BEIAN)){
							errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR.getTypeValue());
							errIp.setStorageType(IpCheckConstants.BEIAN);
							objectInfo.setResult(false);
							objectInfo.setErrInfo(errIp);
							return  objectInfo;
						}else if(bbdw_lydw.getStorageType().equals(IpCheckConstants.UPLOAD)){
							errIp.setConflictType(ConflictType.IP_SOURCE_OVERSTEP_ALO_OMIT_ERROR2.getTypeValue());
							errIp.setStorageType(IpCheckConstants.UPLOAD);
							objectInfo.setResult(false);
							objectInfo.setErrInfo(errIp);
							return  objectInfo;
						}
						continue;
					}
				}//end else
			}else{
				continue;
			}
		}// end for
		
		return objectInfo;
	}


}
